﻿using eShop.Core;
using eShop.Services;
using eShop.Web.Admin.Models;
using eShop.Web.Framework;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace eShop.Web.Admin.controllers
{
    /// <summary>
    /// 入库单信息
    /// </summary>
    public partial class ReceiveController : BaseAdminController
    {
        public ActionResult Receivelist()
        {
            ReceiveModule model = new ReceiveModule();
            model.ReceiveList = AdminStock.AdminGetReceiveList();
            ShopUtils.SetAdminRefererCookie(Url.Action("Receivelist"));
            return View(model);
        }


        /// <summary>
        /// 添加 
        /// </summary>
        [HttpGet]
        public ActionResult Add()
        {
            ReceiveModule model = new ReceiveModule();
            model.ReceiveNo = DateTime.Now.ToString("yyyyMMddHHmmssffffff");
            model.ReceiveDate = DateTime.Now;
            Load();
            return View(model);

        }


        /// <summary>
        /// 导入Excel
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Import()
        {
            Load();
            return View();
        }

        /// <summary>
        /// 添加action
        /// </summary>
        [HttpPost]
        public ActionResult Add(ReceiveModule model)
        {
            //取当前账号
            int uid = ShopUtils.GetUidCookie();
            UserInfo userInfo = AdminUsers.GetUserById(uid);

            if (ModelState.IsValid)
            {
                ReceiveInfo receiveInfo = new ReceiveInfo()
                {
                    ReceiveNo = model.ReceiveNo,
                    PId = model.PId,
                    StockId = model.StockId,
                    ReceiveNum = model.ReceiveNum,
                    DoPerson = userInfo.UserName,
                    ReceiveDate = DateTime.Now,
                    Remark = model.Remark
                };
                if (AdminStock.CreateReceive(receiveInfo) > 0)
                {
                    AddAdminOperateLog("添加入库单", "添加入库单,入库单号:" + model.ReceiveNo);
                    return PromptView("添加入库单成功");
                }
                else
                {
                    return PromptView("添加入库单失败");
                }
            }
            return View(model);
        }

        /// <summary>
        /// 编辑管理员组
        /// </summary>
        [HttpGet]
        public ActionResult Edit(int rid = -1)
        {
            ReceiveInfo receiveInfo = AdminStock.GetReceiveByid(rid);
            if (receiveInfo == null)
                return PromptView("单号不存在");

            ReceiveModule model = new ReceiveModule();
            model.PId = receiveInfo.PId;
            model.StockId = receiveInfo.StockId;
            model.ReceiveNo = receiveInfo.ReceiveNo;
            model.ReceiveNum = receiveInfo.ReceiveNum;
            model.DoPerson = receiveInfo.DoPerson;
            model.Remark = receiveInfo.Remark;
            model.ReceiveDate = receiveInfo.ReceiveDate;

            ViewData["pname"] = AdminProducts.GetProductById(model.PId).Name;
            //
            Load();
            //
            return View(model);

        }

        /// <summary>
        /// 编辑
        /// </summary>
        [HttpPost]
        public ActionResult Edit(ReceiveModule model, int rid = -1)
        {
            if (AdminStock.GetReceiveByid(rid) == null)
                return PromptView("入库单不存在!");
            //开始赋值
            ReceiveInfo receiveInfo = new ReceiveInfo();
            receiveInfo.ReceiveId = rid; //单号
            receiveInfo.ReceiveNo = model.ReceiveNo;
            receiveInfo.PId = model.PId;
            receiveInfo.StockId = model.StockId;
            receiveInfo.ReceiveNum = model.ReceiveNum;
            receiveInfo.ReceiveDate = model.ReceiveDate == null ? DateTime.Now : model.ReceiveDate;
            receiveInfo.DoPerson = WorkContext.UserName;
            receiveInfo.Remark = "修改数据...";
            if (AdminStock.UpdateReceive(receiveInfo) > 0)
            {
                Load();
                return PromptView("入库单修改成功");
            }
            else
            {
                return PromptView("入库单修改失败");
            }

        }

        /// <summary>
        /// 删除
        /// </summary>
        public ActionResult Del(int rid = -1)
        {
            if (AdminStock.DeleteReceiveByid(rid) > 0)
                return PromptView("删除成功!");
            else
                return PromptView("删除失败!");
        }


        /// <summary>
        /// 公共
        /// </summary>
        private void Load()
        {
            List<SelectListItem> stockList = new List<SelectListItem>();
            stockList.Add(new SelectListItem() { Text = "选择所在仓库", Value = "-1" });
            DataTable dt = AdminStock.AdminGetStockList();
            foreach (DataRow dr in dt.Rows)
            {
                stockList.Add(new SelectListItem() { Text = dr["stockmc"].ToString(), Value = dr["stockid"].ToString() });
            }
            ViewData["stockList"] = stockList;
            ViewData["referer"] = ShopUtils.GetAdminRefererCookie();
        }

    }
}
